<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$merchid=$this->checklogin();
if($_RQ['op']=='main'){

	$return['list']=array();
	$return['params']=$return['oldparams']=array('pluginname'=>'');
	$groupparams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
	$groupparams['onepluginname']='';
	$category=pdo_getall('jy_weishop_goods_group',$groupparams,array(),'id','paixu desc');
	foreach($category as &$item){
		$item['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'id','paixu desc');
	}
	//推荐分类
	$groupparams['isrecommand']=1;
	unset($groupparams['parentid']);
	$recommend=pdo_getall('jy_weishop_goods_group',$groupparams);
	$return['recommend']=$return['childs']=$recommend;
	$return['category']=$category;
	exi($return);
}
if($_RQ['op']=='detail'){
	if($_RQ['postdata']){
		//提交表单
		$post=get_postdata();
		$goodsid=intval($post['goodsid']);
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'price'=>$goods['price']+$post['addprice'],
			'costprice'=>$goods['price'],
		);
		//检查是否已添加该商品
		$check=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'api_id'=>$goodsid,'api_pluginname'=>'jy_merch'));
		if($check){
			pdo_update('jy_weishop_goods',$data,array('merchid'=>$merchid,'api_id'=>$goodsid,'api_pluginname'=>'jy_merch'));
			exi('你已代销该商品，修改成功！','','/manage.good_list/');
		}else{
			unset($goods['id']);
			$goods['price']=$data['price'];
			$goods['costprice']=$data['costprice'];
			$goods['merchid']=$merchid;
			$goods['api_id']=$goodsid;
			$goods['api_pluginname']='jy_merch';
			pdo_insert('jy_weishop_goods',$goods);
			exi('代销成功！','','/manage.good_list/');
		}
	}
	$id=intval($_RQ['id']);
	$goods=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','image','price'));
	if(!$goods){
		exi('指定商品不存在！','error');
	}
	$return['item']=array(
		'goodsid'=>$id,
		'addprice'=>1,
	);
	$return['goods']=$goods;
	exi($return);
}